package com.fahai.web.controller;

import com.fahai.utils.json.JsonResult;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.Locale;

import static jdk.nashorn.internal.objects.NativeDebug.map;

//import static io.advantageous.boon.core.Maps.map;

/**
 * Created by sky on 2016/8/22.
 * 把controller里一些公用的移到这里.主要是一些工具函数
 */
@Controller
public class BaseController {
    @Autowired
    MessageSource messageSource;

    /**
     * 把bindResult里的错误信息转换为multivaluemap
     * 这个bindResult必须包含错误才可调用些方法
     */
    public MultiValueMap<String, String> bindResult2map(@NotNull BindingResult bindingResult, @Nullable Locale locale) {
        if (!bindingResult.hasErrors()) {
            throw new IllegalArgumentException("bindingResult 必须要包含error才可调用些方法");
        }
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            map.add(fieldError.getField(), messageSource.getMessage(fieldError, locale));
        }
        return map;
    }

    public MultiValueMap<String, String> bindResult2map(@NotNull BindingResult bindingResult) {
        return bindResult2map(bindingResult, null);
    }

    /**
     * 把bindResult里的错误信息转换为JsonResult
     * 这个bindResult必须包含错误才可调用些方法
     */
    public JsonResult bindResult2JsonResult(@NotNull BindingResult bindingResult, @Nullable Locale locale) {
        MultiValueMap<String, String> data = bindResult2map(bindingResult, locale);
        return new JsonResult(false, "出现错误", data);
    }

    public JsonResult bindResult2JsonResult(@NotNull BindingResult bindingResult) {
        return bindResult2JsonResult(bindingResult, null);
    }

    /**
     * 返回成功
     * @return
     */
    protected JsonResult ok() {
        return new JsonResult(true);
    }

    protected JsonResult ok(String msg) {
        return new JsonResult(true, msg);
    }

    protected JsonResult ok(String msg, Object object) {
        return new JsonResult(true, msg, object);
    }

    protected JsonResult ok(String msg, String k1, Object v1) {
        return new JsonResult(true, msg, map(k1, v1));
    }

    /**
     * 返回失败
     * @return
     */
    protected JsonResult fail() {
        return new JsonResult(false);
    }

    protected JsonResult fail(String msg) {
        return new JsonResult(false, msg);
    }

    protected JsonResult fail(String msg, Object object) {
        return new JsonResult(false, msg, object);
    }

    protected JsonResult fail(String msg, String k1, Object v1) {
        return new JsonResult(false, msg, map(k1, v1));
    }

    /**
     * 返回未登录
     */

    protected JsonResult unlogin(String msg) {
        return new JsonResult(JsonResult.Code.NO_LOGIN,msg);
    }
}